//引入$store,用于后续操作vuex
import $store from "../store/index";
console.log($store)

// import $request from "../util/request";
//创建 audio实例
let audio = new Audio();
audio.playSong = async function (id) {
    audio.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`;
    //当确认歌曲可以播放时,再执行播放
    audio.oncanplay = function () {
        audio.play();
    }
}
audio.addEventListener("pause", () => {
    // console.log("暂停")
    // console.log("当前播放进度" + audio.currentTime)
    $store.state.playPause = false;
})
audio.addEventListener("canplay", () => {
    // console.log("总时长" + audio.duration)

})

audio.addEventListener("timeupdate", () => {
    $store.commit("changeAudio_info", {
        audio_duration: parseInt(audio.duration),
        audio_currentTime: parseInt(audio.currentTime),
    })
})

audio.addEventListener("ended", () => {
    console.log("结束了")
    console.log("播放下一首", $store.state.play_list[$store.state.song_idx - 1]);
    let new_idx;
    if ($store.state.song_idx == $store.state.play_list.length - 1) {
        new_idx = 0;
    } else {
        new_idx = $store.state.song_idx + 1;
    }
    let next_id = $store.state.play_list[new_idx];
    $store.commit("change_idx", new_idx);
    audio.playSong(next_id);
    $store.dispatch("get_song_detail", next_id);
    $store.commit("changeState", true);
})
export default audio;